package com.banzhi.shop.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.model.CallBack.ChangepwdCallBack;
import com.banzhi.shop.model.IChangepwdModel;
import com.banzhi.shop.model.Impl.ChangepwdImpl;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

/**
 * 修改密码
 */
public class ChangePasswordActivity extends BaseActivity implements View.OnClickListener, ChangepwdCallBack {
    /**
     * 返回键
     */
    private ImageView ivback;
    /**
     * 原密码
     */
    private EditText etoldPassword;
    /**
     * 新密码
     */
    private EditText etnewPassword;
    /**
     * 再次新密码
     */
    private EditText etnewPasswordAgain;
    /**
     * 确定
     */
    private Button brntrue;
    private IChangepwdModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        Translucent.setTranslucent(this);
        initViews();
        setListeners();
        model = new ChangepwdImpl();
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        brntrue.setOnClickListener(this);

    }

    private void initViews() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        etoldPassword = (EditText) findViewById(R.id.et_oldpassword);
        etnewPassword = (EditText) findViewById(R.id.et_newPassword);
        etnewPasswordAgain = (EditText) findViewById(R.id.et_newPassword_again);
        brntrue = (Button) findViewById(R.id.btn_true);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_true:
                String password = etnewPassword.getText().toString().trim();
                String passwordagain = etnewPasswordAgain.getText().toString().trim();
                String oldpassword = etoldPassword.getText().toString().trim();
                if (TextUtils.isEmpty(password) || TextUtils.isEmpty(passwordagain) || TextUtils.isEmpty(oldpassword)) {
                    ToastUtils.makeToastShort("请填写完整");
                } else {
                    if (!password.equals(passwordagain)) {
                        ToastUtils.makeToastShort("两次密码输入不一致");
                    } else if (oldpassword.equals(password)) {
                        ToastUtils.makeToastShort("新密码不能和原密码相同");
                    } else {
                        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                        model.getData(oldpassword, password, this);
                    }
                }
        }


    }

    /**
     * @param object 修改密码成功
     */
    @Override
    public void ChangepwdSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        finish();
    }

    /**
     * @param object 修改密码失败
     */
    @Override
    public void ChangepwdError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}